package com.controller.form;

import com.domain.Member;
import com.domain.Space;
import com.domain.User;
import com.server.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
//空间进入跳转控制器
@RequestMapping(value="/index")
public class FormController {

    @Resource(name = "userService")
    private UserService userService;
    @Resource(name = "spaceService")
    private SpaceService spaceService;
    @Resource(name = "discussService")
    private DiscussService discussService;
    @Resource(name = "memberService")
    private MemberService memberService;
    @Resource(name = "contributeService")
    private ContributeService contributeService;
    @Resource(name="resourceService")
    private ResourceService resourceService;



    //通用页面跳转控制器
    @RequestMapping(value="/{formName}")
    public String f(@PathVariable String formName){
        return formName;
    }


    @RequestMapping(value = "/main")
    public String main(Model model, HttpSession session,
                       @RequestParam(value="page",defaultValue="1") int page,
                       @RequestParam(value="pageSize",defaultValue="10") int pageSize) {
        System.out.println("**********进入分页显示******");
        System.out.println("index/mian--页码page==="+page);
        User login_user=(User) session.getAttribute("user");
        int id=login_user.getId();
        User user=userService.selectUserById(id);
        session.setAttribute("user", user);
        model.addAttribute("user",user);
        int user_id=id;
        List<Member> memberList=memberService.selectMemberByUserId(user_id);
        List spaceUser=new ArrayList();
        for(Member member:memberList){
            int space_id=member.getSpace_id();
            Space space1=spaceService.selectSpaceById(space_id);
            spaceUser.add(space1);
        }
        model.addAttribute("spaceUser",spaceUser);
        System.out.println("spaceUser===="+spaceUser);
        //分页：
        List<Space> spaceList=spaceService.findAllSpace(page, pageSize);
        model.addAttribute("spaceList",spaceList);
        //计算总页数：
        int spaceTotal=spaceService.findSpaceTotal();
        System.out.println("index--spaceTotal===="+spaceTotal);
        System.out.println("index控制器--spaceTotal%pageSize==="+(spaceTotal%pageSize));
        if((spaceTotal/pageSize)<1){
            model.addAttribute("pageTotal",1);
        }else{
            if(spaceTotal%pageSize!=0){
                int pageTotal=new Integer(spaceTotal/pageSize+1);
                model.addAttribute("pageTotal",pageTotal);
            }else{
                int pageTotal=new Integer(spaceTotal/pageSize);
                model.addAttribute("pageTotal",pageTotal);
            }
        }
        //设置当前为第一页：
        model.addAttribute("currentPage",page);
        return "main";
    }

    @RequestMapping(value = "/space-main")
    public String spaceMain(Integer space_id,String name,
                            HttpSession session,Model model){
        Space space=spaceService.selectSpaceByNameAndUserId(name,space_id);
        System.out.println("空间space===="+space);
        session.setAttribute("space",space);
        User user2=(User)session.getAttribute("user");
        int id=user2.getId();
        String result=memberService.selectMemberByuser_idAndspace_id(id,space_id);
        System.out.println(result);
        model.addAttribute("result",result);
        //计算资源总数：
        int resourceTotal=resourceService.findResourceTotal(space_id);
        session.setAttribute("resourceTotal",resourceTotal);
        //计算话题总数：
        int discussTotal = discussService.findDiscussTotal(space_id);
        session.setAttribute("discussTotal",discussTotal);
        //计算贡献总数：
        int contributeTotal=contributeService.showContributeTotal(space_id);
        session.setAttribute("contributeTotal",contributeTotal);
        //计算成员总数：
        int memberCount=memberService.findMemberTotal(space_id);
        session.setAttribute("memberCount",memberCount);
        //查询该空间的最新话题：
        List discussList=discussService.descCreateDateLimitPageSize(space_id,8);
        //查询该空间的最新资源
        List reList=resourceService.descCreateDate(space_id);
        //显示空间主页管理团队部分：
        int user_id=spaceService.selectUserIdBySpaceId(space_id);
        User user=userService.selectUserById(user_id);
        System.out.println("空间创建者：==="+user);
        model.addAttribute("user",user);
        model.addAttribute("discussList",discussList);
        model.addAttribute("reList",reList);
        model.addAttribute("space",space);
        session.setAttribute("space",space);
        return "space-main";
    }

    @RequestMapping(value = "/space-manage")
    public String spaceManage(Integer userId,String name,Model model){
        Space space=spaceService.selectSpaceByNameAndUserId(name,userId);
        model.addAttribute("space",space);
        return "space-manage";
    }

}
